// app/controller/users.js
const Controller = require('./baseController')
const { Op } = require('sequelize')

class CategoricalController extends Controller {
	// async list() {
	// 	const { ctx } = this
	// 	const {
	// 		query: { pageSize = 8, currentPage = 1 },
	// 		helper,
	// 	} = ctx
	// 	let queryArr = {
	// 		limit: helper.toInt(pageSize),
	// 		offset: (helper.toInt(currentPage) - 1) * helper.toInt(pageSize),
	// 	}
	// 	const { rows } = await ctx.service.categorical.list(queryArr)
	// 	let res = []
	// 	for (const key in rows) {
	// 		res.push(rows[key].dataValues)
	// 	}

	// 	let arr = helper.Tree2(res)

	// 	helper.success({ data: arr })
	// }
	// 获取所有的列表不分页
	async listAll() {
		const { ctx } = this
		const data = await ctx.service.categorical.listAll()
		ctx.helper.success({ data })
	}

	async list() {
		const { ctx } = this
		const {
			query: { pageSize = 8, currentPage = 1 },
			helper,
		} = ctx
		let queryArr = {
			limit: helper.toInt(pageSize),
			offset: (helper.toInt(currentPage) - 1) * helper.toInt(pageSize),
		}
		const data = await ctx.service.categorical.list(queryArr)

		helper.success({ data })
	}

	// 根据分类名称模糊查询
	async findLike() {
		const { ctx } = this
		const {
			query: { name, status, begin, end },
			helper,
		} = ctx

		let where = {}
		if (status) {
			where['status'] = {
				[Op.eq]: ctx.query.status ? JSON.parse(ctx.query.status) : true,
			}
		}
		if (begin && end) {
			where['createdAt'] = {
				[Op.between]: [ctx.query.begin, ctx.query.end],
			}
		}
		let options = {
			order: [['created_at', 'DESC']],
			where: {
				name: {
					[Op.substring]: name,
				},
				...where,
			},
		}
		const data = await ctx.service.categorical.findLike(options)
		helper.success({ data })
	}
	// 添加分类
	async add() {
		const { ctx } = this
		let { name, pid } = ctx.request.body
		const { helper } = ctx
		if (!name || !name.trim()) {
			return helper.fail({ msg: '分类名称不能够为空！' })
		}
		if (!pid) ctx.request.body.pid = 0
		let options = {
			where: {
				name,
			},
		}
		const res = await ctx.service.categorical.add(ctx.request.body, options)
		if (res.flag) {
			helper.success({ data: res.data.dataValues, msg: '添加成功' })
		} else {
			helper.fail({ msg: res.msg })
		}
	}

	// 删除分类
	async del() {
		const { ctx } = this
		let { ids } = ctx.request.body
		const { helper } = this.ctx
		const res = await ctx.service.categorical.del(ids)
		if (res.flag) {
			helper.success({ msg: '删除成功！' })
		} else {
			helper.fail({ msg: res.msg })
		}
	}

	// 修改分类

	async update() {
		const { ctx } = this
		let {
			request: { body },
			helper,
		} = ctx
		if (!body.id) return helper.fail({ msg: '分类id必填字段！' })

		const odl = await ctx.model.Categorical.findByPk(ctx.request.body.id)

		if (!odl) {
			return helper.fail({ msg: '分类不存在！' })
		}
		const res = await ctx.service.categorical.update(ctx.request.body)
		if (res) {
			helper.success({ msg: '分类修改成功！' })
		} else {
			helper.fail({ msg: '分类修改失败！' })
		}
	}

	// 批量更新
	async updateList() {
		const { ctx } = this
		const res = await ctx.service.categorical.updateList(
			ctx.request.body.categoricals
		)
		if (res.flag && res.count) {
			ctx.helper.success({
				msg: '批量添加成功！',
				inserCount: res.count,
			})
		} else {
			ctx.helper.fail({ msg: '批量添加失败！数据重复！' })
		}
	}
}
module.exports = CategoricalController
